<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 类别管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="handleAdd">新增类别</el-button>
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column label="序号" width="180">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="类别" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="tablePage.pageNum"
                    :page-sizes="pageSizes"
                    :page-size="tablePage.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="tablePage.total"
                >
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="类别名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="resetParam">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="类别名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="resetParam">取 消</el-button>
                <el-button type="primary" @click="saveCate">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            // 分页
            tablePage: {
                pageNum: 1, // 第几页
                pageSize: 5, // 每页多少条
                total: 0 // 总记录数
            },
            pageSizes: [5, 10, 20],
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            addVisible: false,
            pageTotal: 0,
            form: {
                id: 0,
                name: ''
            },
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.$axios
                .post('/admin/catelist', {
                    currPage: this.tablePage.pageNum,
                    pageSize: this.tablePage.pageSize
                })
                .then((r) => {
                    //console.log(r.data.data.data);
                    if (r.data.code == 200) {
                        this.tableData = r.data.data.data;
                        this.tablePage.total = r.data.data.totalNums;
                    }
                });
            // fetchData(this.query).then(res => {
            //     console.log(res);
            //     this.tableData = res.list;
            //     this.pageTotal = res.pageTotal || 50;
            // });
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$axios
                        .post('/admin/delCate', {
                            id: row.id
                        })
                        .then((r) => {
                            if (r.data.code == 200) {
                                this.$message.success('删除成功');
                                //删除成功刷新页面
                                this.getData();
                            }
                        });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            let ids = [];
            for (const row of this.multipleSelection) {
                ids.push(row.id);
            }
            //console.log(ids);
            this.$axios.post('/admin/delAllCate', ids).then((r) => {
                if (r.data.code == 200) {
                    this.$message.error(`删除成功`);
                    this.multipleSelection = [];
                    this.getData();
                }
            });
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form.id = row.id;
            this.form.name = row.name;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.$axios
                .post('/admin/updateCate', {
                    id: this.form.id,
                    name: this.form.name
                })
                .then((r) => {
                    if (r.data.code == 200) {
                        this.editVisible = false;
                        this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                        this.getData();
                        //this.$set(this.tableData, this.idx, this.form);
                    }
                });
        },
        // 改变每页数据大小
        handleSizeChange(val) {
            this.tablePage.pageSize = val;
            this.getData();
            //console.log(`每页 ${val} 条`);
        },
        // 改变某页的数据
        handleCurrentChange(val) {
            this.tablePage.pageNum = val;
            this.getData();
        },
        // 新增类别
        handleAdd() {
            this.addVisible = true;
        },
        // 新增类别
        saveCate() {
            this.$axios
                .post('/admin/addHelperCat', {
                    name: this.form.name
                })
                .then((r) => {
                    if (r.data.code == 200) {
                        this.$message.success('新增成功');
                        this.form.id = 0;
                        this.form.name = '';
                        this.addVisible = false;
                        this.getData();
                    }
                });
        },
        // 重置参数
        resetParam() {
            this.form.id = 0;
            this.form.name = '';
            this.editVisible = false;
            this.addVisible = false;
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-select {
    width: 120px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
